import EventPackage.Event;
import TermioPackage.Termio;


public class SecConsole {
	public static void main(String args[])
	{
		Termio UserInput = new Termio();	// Termio IO Object
		boolean Done = false;				// Main loop flag
		String Option = null;				// Menu choice from user
		Event Evt = null;					// Event object
		boolean Error = false;				// Error flag

		SecMonitor secMonitor = null;		// Security monitor
		boolean armed = false;				// Security on/off status; on = true, off = false
		 
		

		/////////////////////////////////////////////////////////////////////////////////
		// Get the IP address of the event manager
		/////////////////////////////////////////////////////////////////////////////////

		if ( args.length != 0 )
		{
			// event manager is not on the local system

			secMonitor = new SecMonitor ( args [0]);

		} else {

			secMonitor = new SecMonitor();

		} // if


		// Here we check to see if registration worked. If ef is null then the
		// event manager interface was not properly created.

		if (secMonitor.IsRegistered())
		{
			System.out.println("Security Monitor Registered");

			secMonitor.start(); // Here we start the monitoring and control thread

			while (!Done)
			{
				// Here, the main thread continues and provides the main menu

				System.out.println( "\n\n\n\n" );
				System.out.println( "Security (Sec) Command Console: \n" );

				if (args.length != 0)
					System.out.println( "Using event manger at: " + args[0] + "\n" );
				else
					System.out.println( "Using local event manger \n" );

				System.out.println( "Security System Armed: " +armed + "\n");
				System.out.println( "Select an Option: \n" );
				System.out.println( "1: Arm Security" );
				System.out.println( "2: Disarm Security" );
				System.out.println( "3: Activate Sprinklers" );
				System.out.println( "4: Deactivate Sprinklers" );
				System.out.print( "\n>>>> " );
				Option = UserInput.KeyboardReadString();

				//////////// option 1 ////////////

				if ( Option.equals( "1" ) )
				{
					armed = true;
					secMonitor.setArmed(armed);
				} // if

				//////////// option 2 ////////////
				
				else if ( Option.equals( "2" ) )
				{
					armed = false;
					secMonitor.setArmed(armed);
				} // if
				
				//////////// option 3 ////////////
				
				else if ( Option.equals( "3" ) )
				{
					secMonitor.setSprinkler(true);
				} // if

				//////////// option 4 ////////////
				
				else if ( Option.equals( "4" ) )
				{
					secMonitor.setSprinkler(false);
				} // if

				//////////// option X ////////////

				else if ( Option.equalsIgnoreCase( "X" ) )
				{
					// Here the user is done, so we set the Done flag and halt
					// the environmental control system. The monitor provides a method
					// to do this. Its important to have processes release their queues
					// with the event manager. If these queues are not released these
					// become dead queues and they collect events and will eventually
					// cause problems for the event manager.

					secMonitor.Halt();
					Done = true;
					System.out.println( "\nConsole Stopped... Exit monitor mindow to return to command prompt." );
					secMonitor.Halt();

				} // if

			} // while

		} else {

			System.out.println("\n\nUnable start the security monitor.\n\n" );

		} // if

	} // main
}
